package cn.yangliu.nacos.management.entity.oauth2;

import cn.yangliu.nacos.comm.audit.AbstractEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.Type;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * The interface Oauth 2 client.
 *
 * @author 问道于盲
 * @date 2020 -01-01 16:17
 */
@Entity
@Table(name = "oauth2_client")
@Data
public class Oauth2Client extends AbstractEntity implements Serializable {

    /**
     * current class's static member
     * The constant serialVersionUID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * current class instance's member.
     * The Access token validity seconds.
     */
    @Column(name = "access_token_validity_seconds", nullable = false)
    @ApiModelProperty(value = "token失效时间")
    private Long accessTokenValiditySeconds;

    /**
     * current class instance's member.
     * The Authorities.
     */
    @Column(name = "authorities")
    @ApiModelProperty(value = "权限")
    private String authorities;

    /**
     * current class instance's member.
     * The Client id.
     */
    @Column(name = "client_id", nullable = false, unique = true, updatable = false)
    @ApiModelProperty(value = "clientId")
    private String clientId;

    /**
     * current class instance's member.
     * The Client secret.
     */
    @Column(name = "client_secret", nullable = false)
    @ApiModelProperty(value = "secret")
    private String clientSecret;

    /**
     * current class instance's member.
     * The Description.
     */
    @Column(name = "description")
    @ApiModelProperty(value = "描述")
    private String description;

    /**
     * current class instance's member.
     * The Grant types.
     */
    @Column(name = "grant_types", nullable = false)
    @ApiModelProperty(value = "授权方式")
    private String grantTypes;

    /**
     * current class instance's member.
     * The Icon uri.
     */
    @Column(name = "icon_uri")
    @ApiModelProperty(value = "图标")
    private String iconUri;

    /**
     * current class instance's member.
     * The Name.
     */
    @Column(name = "name", nullable = false, unique = true)
    @ApiModelProperty(value = "客户端名称")
    private String name;

    /**
     * current class instance's member.
     * The Redirect uris.
     */
    @Column(name = "redirect_uris", length = 10000)
    @ApiModelProperty(value = "重定向url")
    private String redirectUris;

    /**
     * current class instance's member.
     * The Refresh token validity seconds.
     */
    @Column(name = "refresh_token_validity_seconds", nullable = false)
    @ApiModelProperty(value = "刷新token过期时间")
    private Long refreshTokenValiditySeconds;

    /**
     * current class instance's member.
     * The Scopes.
     */
    @Column(name = "scopes", nullable = false)
    @ApiModelProperty(value = "授权范围")
    private String scopes;

    /**
     * current class instance's member.
     * The Trusted.
     */
    @Column(name = "trusted", nullable = false)
    @ApiModelProperty(value = "是否受信任")
    @Type(type = "true_false")
    private boolean trusted;

    /**
     * current class instance's member.
     * The Roles.
     */
    @ApiModelProperty(value = "角色权限")
    @ManyToMany(cascade = CascadeType.REFRESH, targetEntity = Oauth2Role.class)
    @JoinTable(name = "oauth2_client_role",
            joinColumns = @JoinColumn(name = "client_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    private List<Oauth2Role> roles = new ArrayList<>();

}
